package com.leanyuan.Infor.controller;

import com.leanyuan.Infor.dto.Actiondto;
import com.leanyuan.Infor.api.Result;
import com.leanyuan.Infor.domain.Action;
import com.leanyuan.Infor.service.ActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sys-menu")
public class SysMenuController {
    @Autowired
    ActionService actionService;

    @GetMapping("/ListMenu_role_id")
    public Result<Object> SYS_MENU_LIST(Integer id){
        System.out.println(id);
        List<Action> list=actionService.findAction(id);
        List<Actiondto> actiondto=new ArrayList<Actiondto>();
        menu(actiondto,list,null);
        return Result.ok(actiondto);
    }

    public void menu(List<Actiondto> dtoList,List<Action> actionList,Actiondto dto){
        for(Action action:actionList){
            Integer tempPid=action.getPid();
            Actiondto newdto=new Actiondto(action);
            if(dto==null&&tempPid==null) {
                dtoList.add(newdto);
                if (newdto.getIsLeaf() != 1) {
                    menu(dtoList, actionList, newdto);
                }
            }
                else if(dto!=null && dto.getAction_id().equals(tempPid) && tempPid!=null){
                    dto.getChie().add(newdto);
                    if(newdto.getIsLeaf()!=1){
                        menu(dtoList,actionList,newdto);
                    }

            }
        }
    }
}
